package com.easylinkin.linkappapi.common.log;

import com.alibaba.fastjson.JSONObject;
import com.easylinkin.linkappapi.common.utils.MessageUtils;
import com.easylinkin.linkappapi.operatelog.entity.OperateLog;
import com.easylinkin.linkappapi.operatelog.service.OperateLogService;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import site.morn.bean.annotation.Tag;
import site.morn.log.OperateMeta;
import site.morn.log.Operation;
import site.morn.log.OperationProcessor;

/**
 * <p></p>
 *
 * @author TongJie
 * @since 2020/4/11 19:53
 */
@Component
@Slf4j
@Tag
public class LinkappOperationProcessor implements OperationProcessor {

    @Autowired
    OperateLogService operateLogService;

    @Override
    public void handle(OperateMeta operateMeta, Operation operation) {
        log.info(JSONObject.toJSONString(operation));
//        {"content":"log.设备告警信息Demo.新增方案","date":1586606548723,"module":"设备告警信息Demo","success":true}
        log.info(operateMeta.toString());
//        OperateMeta(source=execution(boolean com.easylinkin.linkappapi.devicedemo.api.controller.DeviceDemoController.addDeviceDemo(DeviceDemo)), success=true, module=设备告警信息Demo, name=新增方案, groupArgs=[], actionArgs=[], codeArgs=[], methodArgs=[com.easylinkin.linkappapi.devicedemo.api.entity.DeviceDemo@6ddc567f], methodReturned=true, throwable=null, excepts=[], attach=null)
        OperateLog operateLog = new OperateLog();
        operateLog.setModuleName(MessageUtils.get(operation.getModule()));
        operateLog.setIp(operation.getIp());
        operateLog.setUrl(operation.getUrl());
        operateLog.setStatus(operation.isSuccess());

        operateLog.setContent(MessageUtils.get(operation.getContent()));
        operateLog.setCreateTime(new Date());
//        operateLog.setCreator(userInfo.getUserName());

        operateLogService.addLog(operateLog);


    }

}